<template>
	<view class="uni-container">
		<uni-forms ref="form" :value="formData" validateTrigger="bind">
			<uni-forms-item name="id" label="店铺编号">
				<uni-easyinput placeholder="创建自动生成" disabled type="number" v-model="formData.id"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="name" label="店铺名称">
				<uni-easyinput placeholder="" v-model="formData.name"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="address" label="地址">
				<uni-easyinput placeholder="店铺详细地址" v-model="formData.address"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="addressName" label="地址简称">
				<uni-easyinput placeholder="店铺地址简称，例如：鸿升大厦" v-model="formData.addressName"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="longitude" label="经度">
				<uni-easyinput placeholder="" v-model="formData.longitude"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="latitude" label="纬度">
				<uni-easyinput placeholder="" v-model="formData.latitude"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="deliveryTypes" label="配送方式">
				<uni-data-checkbox :multiple="true" v-model="formData.deliveryTypes"
					:localdata="formOptions.deliveryTypes_localdata"></uni-data-checkbox>
			</uni-forms-item>
			<uni-forms-item name="notice" label="店铺公告">
				<uni-easyinput placeholder="" v-model="formData.notice"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="phone" label="联系电话">
				<uni-easyinput placeholder="" v-model="formData.phone"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="serviceTime" label="服务时间">
				<uni-easyinput placeholder="" v-model="formData.serviceTime"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="src" label="店铺logo">
				<cloud-image v-model="formData.src"></cloud-image>
			</uni-forms-item>
			<uni-forms-item name="business_license" label="营业执照">
				<cloud-image v-model="formData.business_license"></cloud-image>
			</uni-forms-item>
			<uni-forms-item name="food_license" label="食品经营许可">
				<cloud-image v-model="formData.food_license"></cloud-image>
			</uni-forms-item>
			<uni-forms-item name="uid" label="创建者">
				<uni-easyinput placeholder="" v-model="formData.uid"></uni-easyinput>
			</uni-forms-item>
			<view class="uni-button-group">
				<button type="primary" class="uni-button" style="width: 100px;" @click="submit(2)">通过</button>
				<button type="warn" class="uni-button" style="width: 100px;" @click="submit(3)">拒绝</button>
				<navigator open-type="navigateBack" style="margin-left: 15px;">
					<button class="uni-button" style="width: 100px;">返回</button>
				</navigator>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '../../js_sdk/validator/cloud_shop_apply.js';

	const db = uniCloud.database();
	const dbCmd = db.command;
	const dbCollectionName = 'cloud_shop_apply';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.includes(key)) {
				result[key] = validator[key]
			}
		}
		return result
	}

	export default {
		data() {
			let formData = {
				"address": "",
				"addressName": "",
				"banner": "",
				"deliveryTypes": "deliveryHome",
				"id": null,
				"name": "",
				"notice": "",
				"phone": "",
				"serviceTime": "",
				"src": "",
				"stationId": null,
				"business_license": "",
				"food_license": "",
				"state": null,
				"uid": ""
			}
			return {
				formData,
				formOptions: {
					"deliveryTypes_localdata": [{
							"text": "配送到家",
							"value": "deliveryHome"
						},
						{
							"text": "到店/自提点自提",
							"value": "selfRaising"
						}
					]
				},
				rules: {
					...getValidator(Object.keys(formData))
				}
			}
		},
		onLoad(e) {
			if (e.id) {
				const id = e.id
				this.formDataId = id
				this.getDetail(id)
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			/**
			 * 验证表单并提交
			 */
			submit(type) {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.validate().then((res) => {
					res.state = type
					return this.submitForm(res, type)
				}).catch(() => {}).finally(() => {
					uni.hideLoading()
				})
			},

			/**
			 * 提交表单
			 */
			async submitForm(value, type) {
				// 使用 clientDB 提交数据
				return db.collection(dbCollectionName).doc(this.formDataId).update(value).then(async (res) => {
					if (type == 2) {
						uni.showToast({
							title: '审核成功'
						})
						//判断是否已经存在
						let {
							result
						} = await db.collection("cloud_shops").where("id==" + value.id).get({
							getOne: true
						});
						//调用新增店铺接口
						if (!result.data) {
							await db.action("verify_shop_apply").collection("cloud_shops").add({
								...value,
								"currency": "cny",
								"delivery": {
									"deliveryDay": 4,
									"deliveryPeriod": [],
									"fastestDeliveryDay": 1,
									"freeMoney": 20,
									"maxDistance": 5000,
									"minDistance": 5,
									"minPrice": 20,
									"money": 5,
									"perMoney": 1,
									"time": 30,
									"timeStep": 1800
								},
								state: true
							});
						}
					} else {
						uni.showToast({
							title: '已拒绝'
						})
					}
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			},

			/**
			 * 获取表单数据
			 * @param {Object} id
			 */
			getDetail(id) {
				uni.showLoading({
					mask: true
				})
				db.collection(dbCollectionName).doc(id).field(
					"address,addressName,latitude,longitude,banner,deliveryTypes,id,name,notice,phone,serviceTime,src,stationId,business_license,food_license,state,uid"
				).get().then((res) => {
					const data = res.result.data[0]
					if (data) {
						this.formData = data
					}
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				}).finally(() => {
					uni.hideLoading()
				})
			}
		}
	}
</script>
